package com.hnqcgczy.supplier.service.impl;

import com.hnqcgczy.supplier.dao.AdminDao;
import com.hnqcgczy.supplier.dao.impl.AdminDaoImpl;
import com.hnqcgczy.supplier.db.MyTransaction;
import com.hnqcgczy.supplier.service.AdminService;
import com.hnqcgczy.supplier.vo.Admin;

import java.sql.SQLException;
import java.util.List;

public class AdminServiceImpl implements AdminService {
    private MyTransaction myTransaction = new MyTransaction(true);
    private AdminDao adminDao = new AdminDaoImpl(myTransaction);

    @Override
    public boolean reg(Admin admin) {
        try {
            //先查用户是否被注册,true代表没有被注册过
            boolean flag = adminDao.getUserByName(admin.getAdminName());
            if (!flag) {
                return false;
            }
            //没有被注册，可以用
            boolean reg = adminDao.reg(admin);
            return reg;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Admin login(String username, String password) throws SQLException {
        Admin admin = adminDao.login(username, password);
        return admin;
    }

    @Override
    public boolean updateById(Admin admin) throws SQLException {
        boolean flag = adminDao.updateById(admin);
        return flag;
    }

    @Override
    public boolean deleteById(Integer id) throws SQLException {
        return adminDao.deleteById(id);
    }

    @Override
    public List<Admin> queryAdmin(Admin admin) throws SQLException {
        return adminDao.queryAdmin(admin);
    }
}
